package com.ms.p2p.service.impl;


import com.ms.common.utils.PageData;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.dao.ArticleDao;
import com.ms.p2p.entity.Page;
import com.ms.p2p.entity.content.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl {

    @Autowired
    private ArticleDao articleDao;


    @CacheEvict(value = "article", allEntries = true)
    public int insert(Article map) {
            return articleDao.insert(map);
    }

    @CacheEvict(value = "article", allEntries = true)
    public int update(Article map) {
            return articleDao.update(map);
    }

    @CacheEvict(value = "article", allEntries = true)
    public int delete(int id) {

        return articleDao.deleteById(id);
        // return -1;
    }

    @Cacheable(value = "article", key = "'id'+#id")
    public Article get(String id) {

        return articleDao.get(id);
    }

    public List<Article> getNewsNoticesPage(PageBean page, String title,
                                            String author, String status, String type, String issueDate_begin,
                                            String issueDate_end) {
        return articleDao.findByPage(page.getBegin(), page.getPageSize(),
                title, author, status, type, issueDate_begin, issueDate_end);

    }

    public int selectNewsNoticesCount(String title, String author,
                                      String status, String type, String issueDate_begin,
                                      String issueDate_end) {
        return articleDao.findCount(title, author, status, type,
                issueDate_begin, issueDate_end);
    }

    @Cacheable(value = "article", key = "'type'+#type+'page'+#page.currentPage")
    public List<Article> getIndexNotice(int type, PageBean page) {
        return articleDao.findByPage(page.getBegin(), page.getPageSize(), null,
                null, 1 + "", type + "", null, null);

    }


    @Cacheable(value = "article", key = "'classid'+#classid+'status'+#status+'page'+#page.currentPage")
    public List<Article> findPager(int classid, String status, PageBean page) {
        return articleDao.findByPage(page.getBegin(), page.getPageSize(), null,
                null, status + "", classid + "", null, null);
    }

    @Cacheable(value = "article", key = "'count_'+'classid'+#classid+'status'")
    public int findCount(int classid, String status) {

        return articleDao.findCount(null, null, status + "", classid + "", null, null);
    }

    public List<PageData> listAll(Page pages) {
        return articleDao.datalistAll(pages);
    }
}
